package com.example.myys.data.repository

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.example.myys.Constants
import com.example.myys.Constants.Companion.darkModeState
import com.example.myys.MyApplication
import com.example.myys.data.InventoryDatabase
import com.example.myys.data.dao.SettingLinkDao
import com.example.myys.data.entity.SettingLink
import com.example.myys.util.FileUtil

object SettingLinkRepository {
    private val settingLinkDao: SettingLinkDao = InventoryDatabase.getInstance(MyApplication.instance).settingDao();

    init {
        initSetting()
    }

    //初始化设置
    fun initSetting() {
        val dataYsArrayJson = JSON.parseArray(FileUtil.fileToJsonStr("ysjk.json"))
        for (dataYsJson in dataYsArrayJson) {
            val jsonObject = JSONObject.from(dataYsJson)
            var setting =
                settingLinkDao.getSettingByNameType(jsonObject.getString("name"), Constants.YS)
            if (setting == null) {
                var settingLink = SettingLink(
                    0,
                    jsonObject.getString("name"),
                    Constants.YS,
                    jsonObject.toJSONString()
                )
                settingLinkDao.insert(settingLink)
            } else {
                setting.value = jsonObject.toJSONString()
                settingLinkDao.update(setting)
            }
        }
        val dataXsArrayJson = JSON.parseArray(FileUtil.fileToJsonStr("xsjk.json"))
        for (dataYsJson in dataXsArrayJson) {
            val jsonObject = JSONObject.from(dataYsJson)
            var setting =
                settingLinkDao.getSettingByNameType(jsonObject.getString("name"), Constants.XS)
            if (setting == null) {
                var settingLink = SettingLink(
                    0,
                    jsonObject.getString("name"),
                    Constants.XS,
                    jsonObject.toJSONString()
                )
                settingLinkDao.insert(settingLink)
            } else {
                setting.value = jsonObject.toJSONString()
                settingLinkDao.update(setting)
            }
        }
        val dataMhArrayJson = JSON.parseArray(FileUtil.fileToJsonStr("mhjk.json"))
        for (dataYsJson in dataMhArrayJson) {
            val jsonObject = JSONObject.from(dataYsJson)
            var setting =
                settingLinkDao.getSettingByNameType(jsonObject.getString("name"), Constants.MH)
            if (setting == null) {
                var settingLink = SettingLink(
                    0,
                    jsonObject.getString("name"),
                    Constants.MH,
                    jsonObject.toJSONString()
                )
                settingLinkDao.insert(settingLink)
            } else {
                setting.value = jsonObject.toJSONString()
                settingLinkDao.update(setting)
            }
        }
        //系统个性化设置初始化
        saveSetting(SettingLink(0, "夜间模式", Constants.SYSTEM, ""), true)
    }

    fun getSettingByTypeIsChecked(type: String): SettingLink? {
        return settingLinkDao.getSettingByTypeIsChecked(type)
    }

    fun getSettingByType(type: String): List<SettingLink> {
        return settingLinkDao.getSettingByType(type)
    }
    fun saveSetting(settingLink: SettingLink, isInit: Boolean = false) {
        var setting =
            settingLinkDao.getSettingByNameType(settingLink.name, settingLink.type)
        if (setting == null) {
            settingLinkDao.insert(settingLink)
        } else {
            setting.value = settingLink.value
            if (!isInit) {
                setting.isChecked = settingLink.isChecked
                settingLinkDao.update(setting)
            }
            if (settingLink.name == "夜间模式" && isInit) {
                darkModeState.value = setting.isChecked == 1
            }
        }
    }

    fun updateIsChecked(type: String, settingLink: SettingLink) {
        settingLinkDao.updateAllIsCheckedByType(type)
        settingLinkDao.update(settingLink)
    }
    fun deleteSetting(settingLink: SettingLink) {
        settingLinkDao.delete(settingLink)
    }
}